//
//  main.cpp
//  CH24_02_Stack
//
//  Created by Ge Jian Feng on 14-5-18.
//  Copyright (c) 2014年 Ge Jian Feng. All rights reserved.
//

#include <iostream>

#ifdef __cplusplus
extern "C" {
#endif

#include "Lua/lua.h"
#include "Lua/luaconf.h"
#include "Lua/lualib.h"
#include "Lua/lauxlib.h"
    
#ifdef __cplusplus
}
#endif

static void stackDump(lua_State *L);

int main(int argc, const char * argv[])
{
    lua_State *L = luaL_newstate();
    
    lua_pushboolean(L, 1);
    lua_pushnumber(L, 10);
    lua_pushnil(L);
    lua_pushstring(L, "hello");
    
    stackDump(L);
    
    lua_pushvalue(L, -4);
    stackDump(L);
    
    lua_replace(L, 3);
    stackDump(L);
    
    lua_settop(L, 6);
    stackDump(L);
    
    lua_remove(L, -3);
    stackDump(L);
    
    lua_settop(L, -5);
    stackDump(L);
    
    lua_close(L);
    
    return 0;
}

static void stackDump(lua_State *L)
{
    int i;
    int top = lua_gettop(L);
    for (i = 1; i <= top; i++) {
        int t = lua_type(L, i);
        switch (t) {
            case LUA_TSTRING:
                printf("'%s'", lua_tostring(L, i));
                break;
            case LUA_TBOOLEAN:
                printf(lua_toboolean(L, i) ? "true" : "false");
                break;
            case LUA_TNUMBER:
                printf("%g", lua_tonumber(L, i));
                break;
            default:
                printf("%s", lua_typename(L, t));
                break;
        }
        printf("\t\t");
    }
    printf("\n");
}